

//
//  WZPhotoView.swift
//  weibo
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

let WZPhotoViewReuseIdentifier = "WZPhotoViewReuseIdentifier"



protocol WZPhotoViewDelegate :NSObjectProtocol{
    func deleteImageWithIndex(index:NSIndexPath)
}

class WZPhotoView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate,WZPhotoViewDelegate {

        lazy var imageList:[UIImage] = [UIImage]()
    
    var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    var callBack:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        backgroundColor = UIColor.grayColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private  func setupUI(){
        dataSource = self
        delegate = self
        let itemWH = (KUIScreenW - CGFloat(2) * (Kmargin + KcellPace)) / CGFloat(3)
        print(itemWH)
        
        flowLayout.itemSize = CGSizeMake(itemWH,itemWH )
        flowLayout.minimumInteritemSpacing = KcellPace
        flowLayout.minimumLineSpacing = Kmargin
        registerClass(WZPhotoCell.self, forCellWithReuseIdentifier: WZPhotoViewReuseIdentifier)
        
    }
    
    func addImageToImageList(image:UIImage){
        imageList.append(image)
        reloadData()
    }
    
}
//MARK:实现collectionView的代理方法，数据源方法
extension WZPhotoView{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageList.count == 0 || imageList.count == 9{
            return imageList.count
        }else{
            return imageList.count + 1
        }
        
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WZPhotoViewReuseIdentifier, forIndexPath: indexPath) as! WZPhotoCell
        
        cell.backgroundColor = UIColor.greenColor()
        
        if imageList.count == indexPath.item{//0和9是相等的
            cell.iconImage = nil
            
        } else{//imageList.count = 1..8 是不相等的
             cell.iconImage = imageList[indexPath.item]
        }
//        MARK:c为cell设置代理
       cell.delegate = self
//        根据indexpath删除指定的cell
        cell.indexPath = indexPath
        
        return cell
    }
    
    //    MARK :实现点击cell的代理方法
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if imageList.count == indexPath.item{
            callBack?()
        }
    }
    
    
}
//MARK:实现WZPhotoViewDelegate代理方法
extension WZPhotoView{
    
    func deleteImageWithIndex(index: NSIndexPath) {
        imageList.removeAtIndex(index.item)
        reloadData()
    }
    
}


//MARK:自定义cell
class WZPhotoCell: UICollectionViewCell {
    
    weak var delegate: WZPhotoViewDelegate?

    var indexPath: NSIndexPath?
    var iconImage: UIImage?{
        didSet{
            if iconImage == nil{
                iconImageView.image = UIImage(named: "compose_pic_add_big")
                deleteBtn.hidden = true
            }else{
                iconImageView.image = iconImage
                deleteBtn.hidden = false
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        contentView.addSubview(iconImageView)
        contentView.addSubview(deleteBtn)
        iconImageView.frame = contentView.bounds
        deleteBtn.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
        }
    }
    func deleteBtnClick(){
        if let cellIndexPath = self.indexPath{
            self.delegate?.deleteImageWithIndex(cellIndexPath)
        }
        
    }
    
    lazy var iconImageView:UIImageView = UIImageView()
    
    lazy var deleteBtn: UIButton = {
       let btn = UIButton()
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return btn
    }()
}